package com.atguigu.gulimall.discount_coupon.service.impl;

import com.atguigu.common.to.MemberPrice;
import com.atguigu.common.to.SkuReductionTo;
import com.atguigu.gulimall.discount_coupon.entity.MemberPriceEntity;
import com.atguigu.gulimall.discount_coupon.entity.SkuLadderEntity;
import com.atguigu.gulimall.discount_coupon.service.MemberPriceService;
import com.atguigu.gulimall.discount_coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.discount_coupon.dao.SkuFullReductionDao;
import com.atguigu.gulimall.discount_coupon.entity.SkuFullReductionEntity;
import com.atguigu.gulimall.discount_coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    SkuLadderService skuLadderService;

    @Autowired
    MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReduction(SkuReductionTo reductionTo) {

        //保存sku基本信息
        SkuLadderEntity skuLadderEntity=new SkuLadderEntity();
        BeanUtils.copyProperties(reductionTo,skuLadderEntity);
        skuLadderEntity.setAddOther(reductionTo.getCountStatus());

        skuLadderService.save(skuLadderEntity);


        //保存满减信息
        SkuFullReductionEntity reductionEntity=new SkuFullReductionEntity();
        BeanUtils.copyProperties(reductionTo,reductionEntity);
        this.save(reductionEntity);



        List<MemberPrice> memberPrices = reductionTo.getMemberPrices();

        if (memberPrices!=null&&!memberPrices.isEmpty()){
            List<MemberPriceEntity> collect = memberPrices.stream().map(item -> {
                MemberPriceEntity priceEntity = new MemberPriceEntity();
                priceEntity.setSkuId(reductionTo.getSkuId());
                priceEntity.setMemberLevelId(item.getId());
                priceEntity.setMemberLevelName(item.getName());
                priceEntity.setMemberPrice(item.getPrice());
                priceEntity.setAddOther(reductionTo.getCountStatus());
                return priceEntity;
            }).collect(Collectors.toList());

            memberPriceService.saveBatch(collect);
        }
    }

}